﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestorSQL.Security.Interface;
using System.Security;
using GestorSQL.BusinessEntities;
using GestorSQL.DAO.InterceptDAO;
using AspectIntercept;
using GestorSQL.Utility;

namespace GestorSQL.Security.Implementation.ActiveDirectory
{
    [Intercept(InterceptAllMethods = true, PreProcesor = typeof(DBTraceProcessor), PostProcesor = typeof(DBTraceProcessor), OmitMethods = new string[] { })]
    public class UserManagerMock : InterceptContext, IUserManager
    {
        GestorSQL.BLL.SecurityManager _securityManager = new GestorSQL.BLL.SecurityManager();

        #region IUserManager Members

        public bool Authenticate(string applicationName, string userName, string password)
        {
            password = password.Decrypt();
            if ((userName.ToLower().Equals("aramosb") && password.Equals("1q2w3e4r")) ||
                (userName.ToLower().Equals("carlosm") && password.Equals("1q2w3e4r")) ||
                (userName.ToLower().Equals("daosorio") && password.Equals("1q2w3e4r")) ||
                (userName.ToLower().Equals("jcmendietas") && password.Equals("1q2w3e4r"))
                )
                return true;
            return false;
        }

        [InterceptOmit]
        public bool IsInRole(string applicationName, string userName, string role)
        {

            List<string> roleList = GetRoles(applicationName, userName).ToList();
            bool isInRole = roleList.Contains(role.Trim());

            return isInRole;

        }

        [InterceptOmit]
        public string[] GetRoles(string applicationName, string userName)
        {


            return _securityManager.GetRolesByUserName(userName).Select<Role, string>(r => r.Nombre).ToArray();
        }

        [InterceptOmit]
        public bool ExistUser(string userName, ref string fullName)
        {
            if (string.IsNullOrEmpty(userName))
                return false;

            if (userName.ToLower().Trim().Equals("carlosm"))
            {

                fullName = "Carlos Moya";
                return true;
            }
            else if (userName.ToLower().Trim().Equals("aramosb"))
            {


                fullName = "Andres Mauricio Ramos";
                return true;
            }

            return false;

        }

        #endregion

    }
}
